package gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.Toolkit;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import java.awt.Dimension;
import javax.swing.JLabel;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.HashMap;

import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SwingConstants;

import code.Language;
import code.Settings;
import code.StrongPassword;

public class StrongPasswordMain extends JPanel {
	
	Color generate = new Color(59, 89, 152);

	Color c= new Color(0x80, 0x80, 0xFF);

	Color TAc = new Color(246, 250, 250);

	private static final long serialVersionUID = 1L;
	private JLabel jLabel = null;
	private JLabel jLabel1 = null;

	private JLabel jLabel2 = null;

	private JTextField jTextField = null;

	private JLabel jLabel3 = null;

	private JTextArea jTextArea = null;

	private HashMap labels;
	ATOCOmain atocoframe;

	private JLabel jLabel4 = null;

	private JScrollPane jScrollPane = null;

	private JLabel jLabel31 = null;
	private Settings s;

	/**
	 * This is the default constructor
	 */
	public StrongPasswordMain(ATOCOmain atocoframe) {
		super();
		s = new Settings();
		Language l = new Language();
		labels = l.changeLanguage((String) s.global.get("language"), "StrongPassword", (String) s.global.get("installPath"));
		this.atocoframe=atocoframe;
		initialize();
		this.setPreferredSize( new Dimension( 400, 500 ) );
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		jLabel31 = new JLabel((String) labels.get("jButton"), SwingConstants.CENTER);
		jLabel31.setBounds(new Rectangle(229, 262, 112, 43));
		jLabel31.setBackground(generate);
		jLabel31.setForeground(Color.WHITE);
		jLabel31.setOpaque(true);
		jLabel31.setFont(new Font("Calibri", Font.BOLD, 14));
		jLabel31.addMouseListener(new java.awt.event.MouseAdapter() {
			public void mouseClicked(java.awt.event.MouseEvent e) {
				String input=jTextField.getText();
				if(!input.equals("")){
					try{

						int passlength =Integer.parseInt(input);
						if(passlength>=1&&passlength<=9999){
							StrongPassword sp=new StrongPassword();
							String password=new String(sp.generateStrongPassword(passlength));
							System.out.println(password);
							jTextArea.setText(password);
						}else{
							jTextArea.setText("sifir dan buyuk olmali ve en fazle 4 karakter olabilir");
							Toolkit.getDefaultToolkit().beep();
						}
					}catch(NumberFormatException ex){
						jTextArea.setText("numeric bir deger girin");
						Toolkit.getDefaultToolkit().beep();
					}
				}else{
					String s="istediginiz password uzunluunu girmeniz gerekmektedir";

					jTextArea.setText(s);

				}
			}
		});
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(495, 212, 77, 17));
		jLabel4.setText((String) labels.get("jLabel4"));
		jLabel4.setForeground(c);
		jLabel4.setFont(new java.awt.Font("Arial", Font.BOLD, 10));
//		jLabel3 = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/textarea.gif"));
//		jLabel3.setBounds(new Rectangle(368, 207, 299, 183));
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(27, 221, 220, 27));
		jLabel2.setText((String) labels.get("jLabel2"));
//		jLabel2.setForeground(c);
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(28, 171, 632, 31));
		jLabel1.setText((String) labels.get("jLabel1"));
//		jLabel1.setForeground(c);
		jLabel1.setFont(new java.awt.Font("Dialog", Font.BOLD, 13));
		jLabel = new JLabel(new ImageIcon((String) s.global.get("installPath") + File.separator + "images/en/strongpassword.gif"));
		jLabel.setBounds(new Rectangle(29, 21, 150, 132));
		this.setSize(691, 403);
		this.setLayout(null);
		this.add(jLabel, null);
		this.setBackground(Color.WHITE);
		this.add(jLabel1, null);
		this.add(jLabel2, null);
		this.add(getJTextField(), null);
		//this.add(getJTextArea(), null);
		this.add(jLabel4, null);

		this.add(getJScrollPane(), null);
		this.add(jLabel31, null);
	}

	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getJTextField() {
		if (jTextField == null) {
			jTextField = new JTextField();
			jTextField.setBounds(new Rectangle(256, 222, 87, 26));
			jTextField.addKeyListener(new java.awt.event.KeyAdapter() {
				public void keyPressed(java.awt.event.KeyEvent e) {
					int key = e.getKeyCode();
					if (key == KeyEvent.VK_ENTER) { 
						String input=jTextField.getText();
						if(!input.equals("")){
							try{

								int passlength =Integer.parseInt(input);
								if(passlength>=1&&passlength<=9999){
									StrongPassword sp=new StrongPassword();
									String password=new String(sp.generateStrongPassword(passlength));
									System.out.println(password);
									jTextArea.setText(password);
								}else{
									jTextArea.setText("sifir dan buyuk olmali ve en fazle 4 karakter olabilir");
									Toolkit.getDefaultToolkit().beep();
								}
							}catch(NumberFormatException ex){
								jTextArea.setText("numeric bir deger girin");
								Toolkit.getDefaultToolkit().beep();
							}
						}else{
							String s="istediginiz password uzunluunu girmeniz gerekmektedir";

							jTextArea.setText(s);

						}

					}


				}
			});
		}
		return jTextField;
	}

	/**
	 * This method initializes jTextArea	
	 * 	
	 * @return javax.swing.JTextArea	
	 */
	private JTextArea getJTextArea() {
		if (jTextArea == null) {
			jTextArea = new JTextArea();
			jTextArea.setBounds(new Rectangle(409, 235, 252, 148));
			jTextArea.setBackground(TAc);
			jTextArea.setEditable(false);
			jTextArea.setLineWrap(true);
			jTextArea.setWrapStyleWord(true);
		}
		return jTextArea;
	}

	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setBounds(new Rectangle(409, 235, 252, 148));
			jScrollPane.setViewportView(getJTextArea());
		}
		return jScrollPane;
	}


}  //  @jve:decl-index=0:visual-constraint="10,10"
